#!/usr/bin/perl

use Kaolabo;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);

my $k = Kaolabo->new({
#                        target_file  => 'yukata.jpg', 
#                        target_file  => 'gaijin.jpg', 
                        target_file  => 'party.jpg', 
                        convert_file => time.'.jpg', 
                        uri          => 'https://kaolabo.com/api/detect?apikey=',
                        apikey       => '5ac9d12d9c981b84ce777d28621aa58d',
                    });

$k->access;
$k->effect_face({type=>'line',color=>'#000000'});
#$k->effect_face({type=>'box',color=>'#000000'});

my @point = shuffle @{$k->unface_area()};
my ($x,$y) = (0,0);
#($x,$y) = ($point[0]->{min_x},$point[0]->{min_y})
#  if( @point );

my $img = $k->imager;

my $w = $img->getwidth();
my $h = $img->getheight();
$img->rubthrough(
    src => do {
        my $scalefactor = 1;
        #my $ghost = 'musha.jpg';
        my $ghost = 'hair_m.jpg';
        my $tmp = Imager->new;
        $tmp->read( file => $ghost ) or die $tmp->errstr;

        my $sw = $w/$tmp->getwidth();
        my $sh = $h/$tmp->getheight();
#        $scalefactor = ($sw>$sh)?$sw:$sh if( $sw > 1 || $sh > 1 );

        $tmp = $tmp->scale(scalefactor=>$scalefactor);
        my $alpha = 0.6;

        $tmp = $tmp->convert(matrix =>[ [ 1, 0, 0, 0 ],
                                 [ 0, 1, 0, 0 ],
                                 [ 0, 0, 1, 0 ],
                                 [ 0, 0, 0, $alpha ],
                             ]);
        $tmp;
    },
    tx => $x,
    ty => $y,
);

use Data::Dumper;


warn "----------------------------------------------\n";
#warn Dumper(@{$k->unface_area()});

$k->write('aaa.jpg');
